//
//  StockSectionViewController.m
//  Stock
//
//  Created by blacksun on 11-6-8.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StockSectionViewController.h"
#import "RootViewController.h"


@implementation StockSectionViewController
@synthesize imageView;
@synthesize nameLabel;
@synthesize stockSection;
@synthesize index;
@synthesize parentController;

- (void)dealloc
{
    self.imageView = nil;
    self.nameLabel = nil;
    self.stockSection = nil;
    [super dealloc];
}
-(IBAction)clicked:(id)sender{
    
    [parentController.sectionControllers makeObjectsPerformSelector:@selector(setCliked:) withObject:nil];
    [self setCliked:YES];
    
    if (![StockContext shareInstance].userId) {
        StockAppDelegate *delegate = [UIApplication sharedApplication].delegate;
        [delegate updateRemoteData];
        return;
    }
    
    [[StockContext shareInstance] setWorkingStock:index];
    
    StockAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    Class clazz = NSClassFromString([stockSection objectForKey:kStockController]);
    UIViewController *listViewController = [[clazz alloc] initWithNibName:[stockSection objectForKey:kStockXib] bundle:nil];
    listViewController.title = [stockSection objectForKey:kStockName];
    [appDelegate.navigationController pushViewController:listViewController animated:YES];
    [listViewController release];
}
-(void)setCliked:(BOOL)clicked{
    NSString *imageName = nil;
    if (!clicked) {
        imageName = [stockSection objectForKey:kStockImageFullName];
    }else{
        NSString *orignName = [stockSection objectForKey:kStockImageFullName];
        NSArray *names = [orignName componentsSeparatedByString:@"."];
        imageName = [NSString stringWithFormat:@"%@_gold.png",[names objectAtIndex:0]];
    }
    NSString *imagePath = [[CommonUtil shareInstance] pathOfResouceInMainBundle:imageName];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
    self.imageView.image = image;
    [image release];

}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    self.nameLabel.text = [stockSection objectForKey:kStockName];
    NSString *imagePath = [[CommonUtil shareInstance] pathOfResouceInMainBundle:[stockSection objectForKey:kStockImageFullName]];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
    self.imageView.image = image;
    [image release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.imageView=nil;
    self.nameLabel = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
